package microcredit.gui.handler;

import java.awt.Component;
import java.util.ArrayList;
import java.util.List;

import microcredit.gui.editor.AbstractEditorForm;
import microcredit.gui.editor.EditorPanel;
import microcredit.gui.editor.EditorPanel.EditorDialog;
import microcredit.gui.editor.training.MemberToTrainingTypeForm;
import microcredit.gui.selector.IFilter;
import microcredit.model.Member;
import microcredit.model.MemberToTrainingType;
import microcredit.model.TrainingType;
import microcredit.model.bo.BOTraining;

public class MemberToTrainingHandler extends AbstractHandler<MemberToTrainingType>  {

	private final TrainingType trainingType;
	private final AbstractHandler<Member> memberHandler;
	
	public MemberToTrainingHandler(TrainingType trainingType, AbstractHandler<Member> delegate) {
		super();
		this.memberHandler = delegate;
		this.trainingType = trainingType;
	}

	@Override
	public void choose() throws Throwable {
		memberHandler.choose();
	}
	
	@Override
	public List<IFilter<MemberToTrainingType>> getFilters() {
		return super.getFilters();
	}
	
	@Override
	public MemberToTrainingType open(Component parent, MemberToTrainingType e)
			throws Throwable {
		List<AbstractEditorForm<MemberToTrainingType>> forms = new ArrayList<AbstractEditorForm<MemberToTrainingType>>();
		forms.add( new MemberToTrainingTypeForm());
		
		EditorDialog<MemberToTrainingType> editorDialog =  EditorPanel.createEditorDialog(parent, "Edit", MemberToTrainingType.class, e, this, this, null, forms);
		editorDialog.setVisible(true);
		e = editorDialog.getElement();
		return e;
	}
	
	@Override
	public void delete(MemberToTrainingType e) throws Throwable {
		BOTraining.create().deleteMemberToTraining(e);
	}
	
	@Override
	public MemberToTrainingType save(MemberToTrainingType entity) throws Throwable {
		return BOTraining.create().saveMemberToTraining(entity);
	}

	@Override
	public List<MemberToTrainingType> load() throws Throwable {
		return BOTraining.create().readMembersByTrainingType(trainingType.getId());
	}
	
}
